package cn.yqx.bankshop.oauth2.service.impl;

import cn.yqx.bankshop.oauth2.service.UserService;
import cn.yqx.bankshop.user.pojo.LoginShopUser;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;
@Slf4j
@Component("customUserDetailsService")
public class CustomUserDetailsService implements UserDetailsService {

    @Autowired
    private UserService userService ;

    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {

        LoginShopUser loginShopUser = userService.findByUsername(s);
        log.info(JSON.toJSONString(loginShopUser));
        if (loginShopUser == null) {
            throw new UsernameNotFoundException("帐号未找到=" + s);
        }
        return loginShopUser ;
    }

    public UserDetails loadUserByMobile(String s) {
        LoginShopUser loginShopUser = userService.findByMobile(s);
        if (loginShopUser == null) {
            throw new UsernameNotFoundException("帐号未找到=" + s);
        }
        return loginShopUser ;
    }
}
